﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class BaseData_AddServiceStation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("100100109");
        if (!IsPostBack)
        {
            BindDropProvince();
            BindDropAdmin();
            rdoNo.Checked = true;
        }
    }
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    protected void BindDropProvince()
    {
        List<Model.Province> provinces = new BLL.Province().GetAllList();
        Province.DataSource = provinces;
        Province.DataTextField = "ProName";
        Province.DataValueField = "ProId";
        Province.DataBind();
        ListItem item = new ListItem();
        item.Value = "-1";
        item.Text = "";
        Province.Items.Insert(0, item);
    }

    private void BindDropAdmin()
    {
        List<Model.USERS> users = new BLL.USERS().GetAllList();
        dropAdmin.DataSource = users;
        dropAdmin.DataTextField = "USERS_NAME";
        dropAdmin.DataValueField = "USERS_CODE";
        dropAdmin.DataBind();
        ListItem item = new ListItem();
        item.Value = "-1";
        item.Text = "";
        dropAdmin.Items.Insert(0, item);
    }

    private void ValidShipAddressInfo()
    {
        if (txtServiceStationName.Text == "")
        {
            throw new Exception("请填写服务站名称！");
        }
        if (txtPhone1.Text == "" && txtPhone2.Text == "")
        {
            throw new Exception("联系电话必须至少填写一个！");
        }
        if (txtAddress.Text == "")
        {
            throw new Exception("请填写服务站详细地址！");
        }
        if (dropAdmin.SelectedIndex == 0)
        {
            throw new Exception("请选择服务站管理员！");
        }
    }


    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            if (!Check.CheckPermission("100100109", "00001"))
            {
                Share.Alert(Page, "无权操作"); return;
            }
            ValidShipAddressInfo();
            int id = 0;
            if (new BLL.ServiceSites().InsertNewServiceStation(txtServiceStationName.Text, txtPhone1.Text, txtPhone2.Text, Province.Items[Province.SelectedIndex].Value, hidCityCode.Value, hidAreaCode.Value, txtAddress.Text, dropAdmin.SelectedValue, out id, rdoYes.Checked))
            {
                Share.InsertLog("100100109", "00001");
                Response.Write("<script>alert('新增服务站成功！');window.location.href='ServiceStationsMain.aspx?code=" + id.ToString() + "'</script>");
            }
            else
            {
                Share.Alert(this.Page,"新增服务站失败！");
            }

        }
        catch (Exception ex)
        {
            Share.Alert(this.Page,ex.Message);
        }

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {

    }
}
